/*
 *
 */

#include "gkt_types.h"

#ifndef BB_PROTOCOL_H
#define BB_PROTOCOL_H

/* network layer */
#ifdef GKT_CONFIG_BB_NETWORK_PACKET_MAXLEN
#define BB_NETWORK_PACKET_MAXLEN	\
	GKT_CONFIG_BB_NETWORK_PACKET_MAXLEN
#else
#define BB_NETWORK_PACKET_MAXLEN	512
#endif

#define BB_NETWORK_PREAMBLE	0x55aa
#define BB_NETWORK_VERSION 0x01

//payload = opcode + data
#define BB_NETWORK_PREAMBLE_SIZE 2
#define BB_NETWORK_VERSION_SIZE 1
#define BB_NETWORK_OPCODE_SIZE 1
#define BB_NETWORK_DATA_LENGTH_SIZE	2
#define BB_NETWORK_CHECK_SUM_SIZE 1

#define BB_NETWORK_PAYLOAD_MINSIZE \
	(BB_NETWORK_OPCODE_SIZE + BB_NETWORK_DATA_LENGTH_SIZE)
#define BB_NETWORK_PAYLOAD_OPCODE_OFFSET 0
#define BB_NETWORK_PAYLOAD_DATA_LENGTH_OFFSET \
	(BB_NETWORK_PAYLOAD_OPCODE_OFFSET + BB_NETWORK_OPCODE_SIZE)
#define BB_NETWORK_PAYLOAD_DATA_OFFSET \
	(BB_NETWORK_PAYLOAD_DATA_LENGTH_OFFSET + BB_NETWORK_DATA_LENGTH_SIZE)

#define BB_NETWORK_PREAMBLE_OFFSET	0
#define BB_NETWORK_VERSION_OFFSET \
	(BB_NETWORK_PREAMBLE_OFFSET + BB_NETWORK_PREAMBLE_SIZE)
#define BB_NETWORK_OPCODE_OFFSET \
	(BB_NETWORK_VERSION_OFFSET + BB_NETWORK_VERSION_SIZE)
#define BB_NETWORK_DATA_LENGTH_OFFSET	\
	(BB_NETWORK_OPCODE_OFFSET + BB_NETWORK_OPCODE_SIZE)

#define BB_NETWORK_PACKET_SIZE(data_size)	\
	(BB_NETWORK_PREAMBLE_SIZE + BB_NETWORK_VERSION_SIZE + \
	(data_size) + BB_NETWORK_CHECK_SUM_SIZE)

#define BB_NETWORK_PACKET_MINSIZE	\
	(BB_NETWORK_PREAMBLE_SIZE + BB_NETWORK_VERSION_SIZE \
		+ BB_NETWORK_OPCODE_SIZE + BB_NETWORK_DATA_LENGTH_SIZE \
		+ BB_NETWORK_CHECK_SUM_SIZE)

/* network layer */
int bb_network_send_packet(uint8_t *payload_buff, uint32_t length);
void bb_network_packet_received(const uint8_t *data, uint32_t length);

#endif	/* BB_PROTOCOL_H */

